package com.asia5b.a5mall.shop.activity

import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.logic.viewmodel.shop.ShopCategoryActivityVM
import com.asia5b.a5mall.middleware.model.shop.ShopCategoryModel
import com.asia5b.a5mall.middleware.utils.push
import com.asia5b.a5mall.shop.R
import com.asia5b.a5mall.shop.databinding.ActivityShopcategoryBinding
import com.asia5b.a5mall.shop.fragment.ShopCategoryFragment

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   店铺分类页面
 */
@Route(path = ActCons.ShopCategoryActivity)
class ShopCategoryActivity : SimpleActivity<ShopCategoryActivityVM, ActivityShopcategoryBinding>() {
    override val viewModelClazz: Class<ShopCategoryActivityVM>
        get() = ShopCategoryActivityVM::class.java

    override fun getDataBinding(): ActivityShopcategoryBinding {
        ARouter.getInstance().inject(this)
        shopId = intent.getIntExtra("shopId", 0)
        return ActivityShopcategoryBinding.inflate(layoutInflater)
    }
    override var sIsDark = true

    /**
     * @Name  shopId
     * @Type  Int
     * @Note  店铺id（自动注入）
     */
    @Autowired(name = "shopId", required = true)
    @JvmField
    var shopId: Int = 0

    /**
     * @Name  fragCount
     * @Type  Int
     * @Note  fragment加载个数
     */
    private var fragCount = 1


    override fun initView() {
        mViewModel.loadCategory(shopId)
    }

    override fun initEvent() {
    }

    fun pushFragment(data: ArrayList<ShopCategoryModel>) {
        val fragment = ShopCategoryFragment()
        fragment.mDatas.addAll(data)
        fragCount += 1
        supportFragmentManager
                .beginTransaction()
                .setCustomAnimations(R.anim.push_left_in, R.anim.push_left_out, R.anim.push_right_in, R.anim.push_right_out)
                .add(R.id.shop_category_placeholder, fragment, "$fragCount")
                .addToBackStack(null)
                .commit()
    }

    fun pushActivity(data: ShopCategoryModel) {
        push(ActCons.ShopSearchGoodsActivity, { postcard ->
            postcard.withInt("shopId", shopId)
            postcard.withString("category", "${data.shop_cat_id}")
        })
    }

    override fun startObserver() {
        mViewModel.loadCategoryResultLiveData.observe(this, Observer {
            loadCategoryResult(it)
        })
    }

    override fun initData() {

    }

    override fun backClick(v: View) {
        if (fragCount > 1) {
            fragCount -= 1
            supportFragmentManager.popBackStack()
            return
        }
        super.backClick(v)
    }

    override fun onBackPressed() {
        if (fragCount > 1) {
            fragCount -= 1
            supportFragmentManager.popBackStack()
            return
        }
        super.onBackPressed()
    }

     fun loadCategoryResult(data: ArrayList<ShopCategoryModel>) {
        val fragment = ShopCategoryFragment()
        fragment.mDatas.addAll(data)
        supportFragmentManager.beginTransaction().add(R.id.shop_category_placeholder, fragment, "$fragCount").commit()
    }
}